/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yinsh
 * Date: 2025-02-20
 * Time: 18:48
 */
public class Test {
    public void mergeSortNor (int[] array) {
        partition(array);
    }
    private void partition (int[] array) {
        int gap = 1;
        while (gap < array.length) {
            for (int i = 0; i < array.length; i+=2*gap) {
                int left = i;
                int mid = left+gap-1;
                int right = mid+gap;
                if (mid >= array.length) {
                    mid = array.length-1;
                }
                if (right >= array.length) {
                    right = array.length-1;
                }
                merge(array,left,right,mid);
            }
            gap = gap * 2;
        }
    }

    private void merge(int[] array,int left,int right,int mid) {
        int s1 = left;
        int s2 = mid + 1;
        int[] tampArr = new int[right-left+1];
        int index = 0;
        while (s1 <= mid && s2 <= right) {
            if (array[s1] <= array[s2]) {
                tampArr[index++] = array[s1++];
            }else {
                tampArr[index++] = array[s2++];
            }
        }
        while (s1 <= mid) {
            tampArr[index++] = array[s1++];
        }
        while (s2 <= right) {
            tampArr[index++] = array[s2++];
        }
        for (int i = 0; i < tampArr.length; i++) {
            array[left+i] = tampArr[i];
        }
    }
}
